#include "hm_preview.h"
#include "mydatacenter.h"
#include <EIKENV.H>
/*
 * 用来控制tab的类
 * 
 */
 
extern CMyDataCenter* iDataCenter;	
ChmPreview::ChmPreview()
{
	iOffset = 0;
	iControl = NULL;
	iDownloading = false;
}

ChmPreview::~ChmPreview()
{
	ClearSnapData();
}

void ChmPreview::LoadLargeImage(CHtmlElement* item)
{
	CHtmlElement* itemcontainer = iControl->Element(_L("preview"));//在readnews2.htm中定义了preview
	if(itemcontainer == NULL)
		iLargeImgOrderId = -1;	
		
	//得到当前大图的ID	
	LoadLargeImage(item->Index(itemcontainer) + 1);
}

//imgindex, based 1
void ChmPreview::LoadLargeImage(int imgindex)
{
	iLargeImgOrderId = imgindex;	
	int pageid = iPageId;
	if(iDataCenter->FindPageImage(pageid, imgindex))
	{
		DisplayLargeImg(imgindex);	
	}
	else{
		if(imgindex < 1) return;
		iDownloading = true;
		MY_PREVIEW_DATA* data = iSnapImgData[imgindex-1];
		iDataCenter->DownloadPageImage(data->remotelargename);
	}
}

TKeyResponse ChmPreview::HandleKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType )
{
//CEikonEnv::Static()->InfoWinL(_L("current his"), _L("here2"));
	if(iDownloading)
		return EKeyWasNotConsumed;
		
	if(aKeyEvent.iCode == EKeyLeftArrow || aKeyEvent.iCode == EKeyUpArrow)
	{
		int index = iLargeImgOrderId;
		if(index == 1){
			iDataCenter->iWaitDlg.Note(_L("It's the 1'st"), 1);
			return EKeyWasNotConsumed;
		}
//		CEikonEnv::Static()->InfoMsg(_L("prev"));
		LoadLargeImage(index-1);
		return EKeyWasConsumed; 
	}
	
	if(aKeyEvent.iCode == EKeyRightArrow || aKeyEvent.iCode == EKeyDownArrow)
	{
		int index = iLargeImgOrderId;
		if(index == iSnapImgData.Count()){
			iDataCenter->iWaitDlg.Note(_L("It's the last"), 1);			
			return EKeyWasNotConsumed;
		}
//		CEikonEnv::Static()->InfoMsg(_L("next"));
		LoadLargeImage(index+1);
		return EKeyWasConsumed; 
	}
	
	return EKeyWasNotConsumed;	
}

void ChmPreview::Init(CHtmlControl* aControl, int aImageNum, int aPageId, int aChannelId)
{
//TBuf<100> tt;
//tt.Format(_L("%d, %d, %d"), aImageNum, aPageId, aChannelId);
//
//CEikonEnv::Static()->InfoWinL(_L("test"), tt);	
	//
	iControl = aControl;
	iImageNum = aImageNum;
	iPageId = aPageId;
	
	//生成每个snap图的信息
	int pageid = aPageId;

	for(int i = 0; i < iImageNum; i++)
	{
		int imgindex = i+1;
		TBuf<128> localfile;
		_LIT(KMemPreviewImg, "mem.%06d_%02d_s.jpg");
		localfile.Format(KMemPreviewImg, pageid, imgindex);

		TBuf<128> localfile2;
		_LIT(KMemLargeImg, "mem.%06d_%02d_m.jpg");
		localfile2.Format(KMemLargeImg, pageid, imgindex);
		
		int subdir = pageid/500;
		TBuf8<256> url;
		_LIT8(KRemotePreImg, "http://221.130.184.102/moimages/%04d/%06d/%06d_%02d_s.jpg");
		url.Format(KRemotePreImg, aChannelId, subdir, pageid, imgindex);

		TBuf8<256> url2;
		_LIT8(KRemoteLargeImg, "http://221.130.184.102/moimages/%04d/%06d/%06d_%02d_m.jpg");
		url2.Format(KRemoteLargeImg, aChannelId, subdir, pageid, imgindex);
		
		MY_PREVIEW_DATA* data = new MY_PREVIEW_DATA;
		data->memsnapname.Copy(localfile);
		data->memlargename.Copy(localfile2);
		
		data->remotesnapname.Copy(url);		
		data->remotelargename.Copy(url2);				
		data->showed = false;
		data->imgid = i;
		imgindex ++;
		iSnapImgData.Append(data);
	}
	
	
	CHtmlElement* itemcontainer = iControl->Element(_L("preview"));
	if(itemcontainer == NULL)
		return;
	else
		itemcontainer->SetProperty(KHStrInnerHtml, KNullDesC);
	
	//_LIT(KImageHtml, "<img id='snapimg' src='mem.preview.gif' width='32' height='32'>");		
	_LIT(KImageHtml, "<div id='snapimg' style='width:50;height:50;border:1;background-image:mem.preview.gif;background-position:center center; '"
	 "focusing='true' class='cat' focus-class='prefocus'><div id='link' style='display:none'></div></div>");
	
	for(int i = 0; i < iImageNum; i++){
		CHtmlElement* image = iControl->InsertContentL(itemcontainer, EBeforeEnd, KImageHtml);
		MY_PREVIEW_DATA* data = iSnapImgData[i];
		CHtmlElement* item = image->Element(_L("link"));
		item->SetProperty(KHStrInnerText, data->remotelargename);
	}

}
int ChmPreview::NextImgToShow()
{
	int res = -1;
	for(int i = 0; i < iSnapImgData.Count(); i ++)
	{
		MY_PREVIEW_DATA* data = iSnapImgData[i];
		if(data->showed == false)
		{
			res = i;
			break;
		}
	}
	return res;
}

//
void ChmPreview::SetSnapImg(int imgindex)
{
	if(iSnapImgData.Count() == 0) return;
	
	MY_PREVIEW_DATA* data = iSnapImgData[imgindex];	
	CHtmlElement* preview = iControl->Element(_L("snapimg"),imgindex);
	if(preview)
	{
		preview->SetStyle(KHStrBackgroundImage, data->memsnapname);
		data->showed = true;
	}
}

void ChmPreview::DisplayLargeImg(int imgindex)
{
	if(iSnapImgData.Count() == 0) return;
	if(imgindex < 1) return;

	iDownloading = false;	
	MY_PREVIEW_DATA* data = iSnapImgData[imgindex-1];	
	CHtmlElement* view = iControl->Element(_L("iFloat"));
	if(view)
	{
		view->SetStyle(KHStrBackgroundImage, data->memlargename);
		view->SetStyle(KHStrDisplay, KHStrVisible);
//		CEikonEnv::Static()->InfoMsg(data->memlargename);
		iControl->RefreshAndDraw();		
	}
}

void ChmPreview::ShowTabsChange()
{
}

bool ChmPreview::ProcessTabChange(bool bNextPrev)
{
	return true;
}

void ChmPreview::ClearSnapData()
{
	if(iControl == NULL)
	return;
	for(int i = 0; i < iSnapImgData.Count(); i++)
	{
		MY_PREVIEW_DATA* data = (MY_PREVIEW_DATA*)iSnapImgData[i];
		delete data;
	}

	iSnapImgData.Reset();
}
